<script type="text/javascript" src="/application/js/jquery.itextclear.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $('input[type=text], input[type=password], input[type=url], input[type=email], input[type=number], textarea', '.form').iTextClear();
});
</script>
<h1 class="page-title">+1</h1>
<div class="container_12 clearfix leading">
	<input type="hidden" id="id_diet" value="<?php echo $id_diet; ?>"/>
	<input type="hidden" id="id_patient" value="<?php echo $id_patient; ?>"/>
	<div class="grid_12">
		<form class="form" action="" method="POST">
			<div class="clearfix">
				<label for="form-food-id" class="form-label">Alimento<em>*</em></label>
				<div class="form-input">
					<select id="form-food-id" name="food-id" required="required">
						<option value="0">Novo Alimento</option>
						<?php
							foreach ($all_f as $f){
								echo '<option value="'.$f['id'].'">'.$f['name'].'</option>';
							}
						?>
					</select>
				</div>
			</div>
			<div class="clearfix">
				<label for="form-name" class="form-label">Nome:</label>
				<div class="form-input"><input class="new_food" id="form-name" name="name" type="text"  value="" placeholder="Nome"/></div>
			</div>
			<div class="clearfix">
				<label for="form-kcal" class="form-label">Kcal:</label>
				<div class="form-input"><input class="new_food" id="form-kcal" name="kcal" type="text"  value="" placeholder="kcal"/></div>
			</div>
			<div id="measure-select" class="clearfix">
				<label for="form-measure-select" class="form-label">Medida:</label>
				<div class="form-input">
					<select id="form-measure-select" name="measure-select" required="required">
						<option value="">Medida</option>
							<?php
								foreach ($all_m as $m){
									echo '<option value="'.$m['m_id'].'">'.$m['m_name'].'</option>';
								}
							?>
					</select>
				</div>
			</div>
			<div  id="measure-input" class="clearfix">
				<label for="form-measure-input" class="form-label" >Medida:</label>
				<div class="form-input">
					<input type="hidden" name="id-measure" id="id-measure" value=""/>
					<input type="text" name="measure-input" id="form-measure-input" class="new_food" value="" placeholder="Medida"/>
				</div>
			</div>
			<div class="clearfix">
				<label for="form-gml" class="form-label">g/ml na medida:</label>
				<div class="form-input"><input class="new_food" id="form-gml" name="gml" type="text"  value="" placeholder="g/ml na medida"/></div>
			</div>
			<div class="clearfix">
				<label for="form-time-id" class="form-label">Horario:</label>
				<div class="form-input">
					<select id="form-time-id" name="time-id" required="required">
						<option value="">Horário</option>
							<?php
								foreach ($all_t as $t){
									echo '<option value="'.$t['id'].'">'.$t['name'].'</option>';
								}
							?>
					</select>
				</div>
			</div>
			<div class="clearfix">
				<label for="form-amount" class="form-label">Quantidade:</label>
				<div class="form-input"><input class="new_food" id="form-amount" name="amount" type="text"  pattern="[0-9]*"  placeholder="quantidade" value=""/></div>
			</div>

			<div class="form-action clearfix">
				<div class="button submit">Adicionar a Dieta</div>
			</div>
		</form>
	</div>
</div>
<script>
	$(document).ready(function(){		
		$('#measure-input').hide();
		
		$('#form-food-id').change(function(){
			if($(this).val()=='0'){
//				$('.new_food').val('');
//				$('.new_food').removeAttr('readonly');
//				$('#box-name-food .ui-btn-text').html('Novo Alimento<span class="ui-collapsible-heading-status"> click to expand contents</span>');
				$('#measure-input').hide();
				$('#measure-select').show();
				
			}else{
				$('#measure-input').show();
				$('#measure-select').hide();
				data = {
					id_food:$(this).val()
				}

				$.post('/ajax/get_food',data,function(r){
					$('#form-name').val(r.name);				
					$('#form-kcal').val(r.kcal);				
					$('#form-measure-input').val(r.m_name);
					$('#form-gml').val(r.gml);
//					$('#new_food-carb').val(r.carb);				
//					$('#new_food-protein').val(r.protein);				
//					$('#new_food-fat_t').val(r.fat_t);				
//					$('#new_food-fat_s').val(r.fat_s);				
//					$('#new_food-fat_tr').val(r.fat_tr);				
//					$('#new_food-fiber').val(r.fiber);				
//					$('#new_food-sodium').val(r.sodium);
				},'json');
//				$('.new_food').attr('readonly','readonly'); 
			}
		});
		
		$('.button.submit').click(function(){
			if($('#form-food-id').val()=='0'){ //novo alimento
				data = {
					new_food	: true,
					id_food		: $('#form-food-idfood').val(),
					name		: $('#form-name').val(),
					id_measure	: $('#form-measure-select').val(),
					gml			: $('#form-gml').val(),
					kcal		: $('#form-kcal').val(),
//					protein		: $('#new_food-protein').val(),
//					carb		: $('#new_food-carb').val(),
//					fat_t		: $('#new_food-fat_t').val(),
//					fat_s		: $('#new_food-fat_s').val(),
//					fat_tr		: $('#new_food-fat_tr').val(),
//					fiber		: $('#new_food-fiber').val(),
//					sodium		: $('#new_food-sodium').val(),
					new_id_patient : $('#id_patient').val(),
					id_time		: $('#form-time-id').val(),
					id_diet		: $('#id_diet').val(),
					amount		: $('#form-amount').val()
				}
				$.post('/ajax/insert_new_diet_check',data,function(r){
					location.hash = "#dieta";
				});
			}else{
				data = {
					id_diet		: $('#id_diet').val(),
					id_food		: $('#form-food-id').val(),
					id_time		: $('#form-time-id').val(),
					amount		: $('#form-amount').val()
				}
				$.post('/ajax/insert_new_diet_check',data,function(r){
					location.hash = "#dieta";
				});
			}
		});	
	});
</script>